import http, {ApiResponse, ApiResponsePage} from "../../../../lang/http";

/**
 * 对象字段
 */
export interface Menu {
    // ID
    id: string;
    // 父级ID
    pid: string;
    // 层级路径
    path: string;
    // 层级
    level: string;
    // 图标
    icon: string;
    // 菜单名称
    name: string;
    // 标题
    title: string;
    // 路径
    src: string;
    // 描述
    description: string;
    // 类型
    type: string;
    // 排序
    orderBy: string;
    // 授权码
    authorityCode: string;
    // 是否可编辑
    editable: boolean;
    // 是否可用
    disabled: boolean;
    // 组别
    groupName: string;
}

/**
 * 创建一个基础对象，用于构建表单，需要包含必要的默认值
 */
const formModel = (): Menu => {
    return {
        //ID
        id: ''
        //父级ID
        , pid: ''
        //层级路径
        , path: ''
        //层级
        , level: ''
        //图标
        , icon: ''
        //菜单名称
        , name: ''
        //标题
        , title: ''
        //路径
        , src: ''
        //描述
        , description: ''
        //类型
        , type: ''
        //排序
        , orderBy: ''
        //授权码
        , authorityCode: ''
        //是否可编辑
        , editable: true
        //是否可用
        , disabled: true
        //组别
        , groupName: 'default'
    }

}

/**
 * 表单校验规则
 */
const formRules = {
    id: [
        {required: true, message: "[ID]字段不允许为空"},
        {min: 0, max: 32, message: "[ID]最多32字符"}
    ],
    pid: [
        {min: 0, max: 32, message: "[父级ID]最多32字符"}
    ],
    path: [
        {min: 0, max: 32, message: "[层级路径]最多32字符"}
    ],
    icon: [
        {min: 0, max: 32, message: "[图标]最多32字符"}
    ],
    name: [
        {min: 0, max: 32, message: "[菜单名称]最多16字符"}
    ],
    title: [
        {min: 0, max: 32, message: "[标题]最多32字符"}
    ],
    src: [
        {min: 0, max: 32, message: "[路径]最多128字符"}
    ],
    description: [
        {min: 0, max: 32, message: "[描述]最多128字符"}
    ],
    type: [
        {min: 0, max: 32, message: "[类型]最多16字符"}
    ],
    authorityCode: [
        {min: 0, max: 32, message: "[授权码]最多32字符"}
    ],
    groupName: [
        {min: 0, max: 32, message: "[组别]最多64字符"}
    ],
}

/**
 * 默认的数据映射函数
 */
const mapper_menus: TreeMapper = (ele: unknown, ret: TreeNode): void => {
    ret.data = ele;

    ret.id = ele.id;
    ret.pid = ele.pid;
    ret.level = ele.level;

    ret.icon = ele.icon;
    ret.label = ele.title;
    ret.checked = ele.checked;
}


/**
 * 新建
 * @param pid 父级 ID
 * @returns 请求Promise
 */
const create = (pid): Promise<ApiResponse> => {
    return http.ajax({
        url: '/sys/menu/info',
        method: 'POST',
        params: {pid}
    });
}

/**
 * 删除
 * @param id ID
 * @returns 请求Promise
 */
const remove = (id: string | number): Promise<ApiResponse> => {
    return http.ajax({
        method: "DELETE",
        url: 'sys/menu/info',
        params: {id}
    });
}

/**
 * 快速编辑字典配置
 * @param params 字段信息
 * @returns 请求Promise
 */
const modify = (params: Menu): Promise<ApiResponse> => {
    return http.ajax({
        method: "PUT",
        url: 'sys/menu/info',
        params: params
    });
}

/**
 * 查询列表
 * @param params 查询参数
 * @returns 分页数据Promise
 */
const list = (params: object): Promise<ApiResponsePage> => {
    return http.ajax({
        url: '/sys/menu/list',
        method: 'GET',
        params
    });
}

export default {create, remove, modify, list, formModel, formRules, mapper_menus}